﻿using Brc_PaaS.Common.Helper;
using Brc_PaaS.Common.Model.Basic;
using Brc_PaaS.Common.Model.RabbitMq;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;

namespace Brc_PaaS.Core.RabbitMq
{
    public class RabbitMqBasic: IRabbitMqBasic
    {
        /// <summary>
        /// 新增用户接口
        /// </summary>
        /// <param name="configInfoDto"></param>
        /// <returns></returns>
        public BaseResponseModel CreateUser(RabbitMqCreateUserDto createUserDto)
        {
            var result = new BaseResponseModel { IsSuccess = false };
            if (createUserDto == null)
            {
                result.Msg = "参数对象为空";
                return result;
            }
            string url = string.Format("http://{0}/api/users/{1}",createUserDto.RequestUrl,createUserDto.UserName);
            string postData = JsonConvert.SerializeObject(createUserDto);
            var header = GetHeader(createUserDto.LoginUserName, createUserDto.LoginPassword);
            var strConfigDto = HttpHelper.HttpPut(url, postData,header);
            if(string.IsNullOrWhiteSpace(strConfigDto))
            {
                result.IsSuccess = true;
            }
            else
            {
                result.Msg = strConfigDto;
            }
            return result;
        }
        /// <summary>
        /// 删除用户接口
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="basicRequestDto"></param>
        /// <returns></returns>
        public BaseResponseModel DeleteUser(string userName,RabbitMqBasicRequestDto basicRequestDto)
        {
            var result = new BaseResponseModel() { IsSuccess=false};
            if(string.IsNullOrWhiteSpace(userName))
            {
                result.Msg = "参数为空";
                return result;
            }
            string url = string.Format("http://{0}/api/users/{1}", basicRequestDto.RequestUrl, userName);
            var header = GetHeader(basicRequestDto.LoginUserName, basicRequestDto.LoginPassword);
            var strConfigDto = HttpHelper.HttpDelete(url, null, header);

            if (string.IsNullOrWhiteSpace(strConfigDto))
            {
                result.IsSuccess = true;
            }
            else
            {
                result.Msg = strConfigDto;
            }
            return result;
        }

        /// <summary>
        /// 创建虚拟机
        /// </summary>
        /// <param name="createVitrualHostDto"></param>
        /// <returns></returns>
        public BaseResponseModel CreateVirtualHost(RabbitMqCreateVitrualHostDto createVitrualHostDto)
        {
             var result = new BaseResponseModel { IsSuccess = false };
            if (createVitrualHostDto == null)
            {
                result.Msg = "参数对象为空";
                return result;
            }
            string url = string.Format("http://{0}/api/vhosts/{1}", createVitrualHostDto.RequestUrl, createVitrualHostDto.VirtualHostName);
            string postData = JsonConvert.SerializeObject(createVitrualHostDto);
            var header = GetHeader(createVitrualHostDto.LoginUserName, createVitrualHostDto.LoginPassword);
            var strConfigDto = HttpHelper.HttpPut(url, postData,header);

            if (string.IsNullOrWhiteSpace(strConfigDto))
            {
                result.IsSuccess = true;
            }
            else
            {
                result.Msg = strConfigDto;
            }
            return result;
        }
        /// <summary>
        /// 删除虚拟机
        /// </summary>
        /// <param name="virtualHostName"></param>
        /// <param name="basicRequestDto"></param>
        /// <returns></returns>
        public BaseResponseModel DeleteVirtualHost(string virtualHostName,RabbitMqBasicRequestDto basicRequestDto)
        {
            var result = new BaseResponseModel { IsSuccess = false };
            if (basicRequestDto == null|| string.IsNullOrWhiteSpace(virtualHostName))
            {
                result.Msg = "参数对象为空";
                return result;
            }
            string url = string.Format("http://{0}/api/vhosts/{1}", basicRequestDto.RequestUrl, virtualHostName);
            var header = GetHeader(basicRequestDto.LoginUserName, basicRequestDto.LoginPassword);
            var strConfigDto = HttpHelper.HttpDelete(url, null, header);

            if (string.IsNullOrWhiteSpace(strConfigDto))
            {
                result.IsSuccess = true;
            }
            else
            {
                result.Msg = strConfigDto;
            }
            return result;
        }

        /// <summary>
        /// 给用户分配虚拟机的权限
        /// </summary>
        /// <param name="allocatePermissionDto"></param>
        /// <returns></returns>
        public BaseResponseModel AllocatePermission(RabbitMqAllocatePermissionDto allocatePermissionDto)
        {
            var result = new BaseResponseModel { IsSuccess = false };
            if (allocatePermissionDto == null)
            {
                result.Msg = "参数对象为空";
                return result;
            }
            string url = string.Format("http://{0}/api/permissions/{1}/{2}", allocatePermissionDto.RequestUrl, allocatePermissionDto.VirtualHostName,allocatePermissionDto.UserName);
            string postData = JsonConvert.SerializeObject(allocatePermissionDto.Permission);
            var header = GetHeader(allocatePermissionDto.LoginUserName, allocatePermissionDto.LoginPassword);
            var strConfigDto = HttpHelper.HttpPut(url, postData,header);

            if (string.IsNullOrWhiteSpace(strConfigDto))
            {
                result.IsSuccess = true;
            }
            else
            {
                result.Msg = strConfigDto;
            }
            return result;
        }


        private Dictionary<string,string> GetHeader(string loginUserName,string loginPassword)
        {
            var header = new Dictionary<string, string>();
            string auth = loginUserName + ":" + loginPassword;
            header.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(auth)));
            return header;
        }
    }
}
